package com.epoint.mobileoa.tools.controls.tree.codeitemtree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ProgressDialog;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.epoint.mobileoa.R;
import com.epoint.mobileoa.bizlogic.SuperView;
import com.epoint.mobileoa.bizlogic.Task;
import com.epoint.mobileoa.db.DBHelp;
import com.epoint.mobileoa.service.MainService;
import com.epoint.mobileoa.tools.controls.tree.Node;
import com.epoint.mobileoa.tools.controls.tree.TreeAdapter;

/**
 * @author liyc
 * @time 2012-7-7 上午9:50:03
 * @annotation 代码项生成树
 */
public class CodeItemTree extends SuperView{

	public static List<Map<String,String>> chooseNodeData = new ArrayList<Map<String,String>>();
	private ProgressDialog m_Dialog;
	private ListView lv;

	@Override
	public void initControls() {
		lv = (ListView) findViewById(R.id.code_list);
		lv.setOnItemClickListener(this);
		top_right_bt.setVisibility(View.VISIBLE);
		top_right_bt.setOnClickListener(this);
		top_right_bt.setText("确定");		
		String viewTitle = getIntent().getStringExtra("viewTitle");
		top_title.setText(viewTitle);
		
		getData();
	}

	private void getData(){
		showProgressDialog();
		HashMap<String, Object> param = new HashMap<String, Object>();
		param.put("CodeName", getIntent().getStringExtra("CodeName"));
		param.put("rootitemvalue", getIntent().getStringExtra("rootitemvalue"));
		param.put("style", getIntent().getStringExtra("style"));
		param.put("type", getIntent().getIntExtra("type", 0));
		param.put("unleafchoose", getIntent().getBooleanExtra("unleafchoose", false));
		param.put("nodeKey", getIntent().getStringExtra("nodeKey"));
		param.put("backgetDataMethod", getIntent().getStringExtra("backgetDataMethod"));
		//获取后台树数据
		Task task = new InitCodeItemTreeTask(this, param);
		MainService.addTask(task);
	}
	
	private void showProgressDialog(){
		m_Dialog = new ProgressDialog(this);
		m_Dialog.setMessage("正在加载数据...");	
		m_Dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		m_Dialog.setCancelable(true);
		m_Dialog.show();
	}

	@Override
	public void refresh(Object... param) {
		m_Dialog.dismiss();
		if(param[0].toString().equals(MainService.NORMAL)){
			lv.setAdapter((ListAdapter) param[1]);
		}
	}

	@Override
	public int getLayoutInt() {
		return R.layout.tree;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		((TreeAdapter)parent.getAdapter()).ExpandOrCollapse(position);
	}

	@Override
	public void onClick(View v) {
		if(v==top_right_bt){
			chooseNodeData.clear();

			Map<String,String> map;

			List<Node> nodes = ((TreeAdapter)lv.getAdapter()).getSeletedNodes();

			for(int i=0;i<nodes.size();i++){

				Node n = nodes.get(i);

				boolean isleaf = true;

				/*if(!unleafchoose){
					isleaf = n.isLeaf();
				}*/

				if(isleaf){
					map = new HashMap<String, String>();
					map.put("text", n.getText());
					map.put("param1", n.getParam1());
					map.put("param2", n.getParam2());
					chooseNodeData.add(map);
				}
			}
			finish();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 1, 1, "刷新数据");
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case 1:
			DBHelp.deleteResData(this, getIntent().getStringExtra("nodeKey"));
			getData();
			break;
		}
		return true;
	}

	@Override
	public void refreshForNewTask(int TaskId,Object... param) {
	}
}
